home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / viewpbm.ps < prev    next >
Text File  |  1996-01-10  |  4KB  |  125 lines

  1. %    Copyright (C) 1992, 1995 Aladdin Enterprises.  All rights reserved.
  2. %
  3. % This file is part of Ghostscript.
  4. %
  5. % Ghostscript is distributed in the hope that it will be useful, but
  6. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. % to anyone for the consequences of using it or for whether it serves any
  8. % particular purpose or works at all, unless he says so in writing.  Refer
  9. % to the Ghostscript General Public License for full details.
  10. %
  11. % Everyone is granted permission to copy, modify and redistribute
  12. % Ghostscript, but only under the conditions described in the Ghostscript
  13. % General Public License.  A copy of this license is supposed to have been
  14. % given to you along with Ghostscript so you can know your rights and
  15. % responsibilities.  It should be in a file named COPYING.  Among other
  16. % things, the copyright notice and this notice must be preserved on all
  17. % copies.
  18.  
  19. % viewpbm.ps
  20. % Display a PBM/PGM/PPM file.  Currently only handles "raw" formats (P4-P6).
  21. % Requires the Level 2 `image' operator (to handle variable pixel widths).
  22. % If SCALE is defined, maps input pixels to output pixels with that scale;
  23. % if SCALE is undefined, scales the image to fit the page.
  24.  
  25. /s 100 string def
  26. /readmaxv
  27.  { f s readline pop cvx exec /maxv exch def
  28.  } bind def
  29. /readrow
  30.  { 0 1 row length 1 sub { row exch f token pop put } for
  31.  } bind def
  32. /read01            % <count> read01 <byte>
  33.  { 0 exch { f read pop 48 xor dup 1 le { exch dup add add } { pop } ifelse } repeat
  34.  } bind def
  35. /pdict 6 dict def
  36. pdict begin
  37.   /P1 [ { /maxv 1 def /rsize w 7 add 8 idiv def
  38.       /wrem w 8 mod def
  39.           /ncomp 1 def /invert true def
  40.     }
  41.     { 0 1 w 8 idiv { row exch 8 read01 put } for
  42.       wrem 0 ne
  43.        { row rsize 1 sub wrem read01 8 wrem sub bitshift put
  44.        } if
  45.       row
  46.     }
  47.       ] def
  48.   /P2 [ { readmaxv /rsize w def
  49.       /ncomp 1 def /invert false def
  50.     }
  51.     { readrow row }
  52.       ] def
  53.   /P3 [ { readmaxv /rsize w 3 mul def
  54.           /ncomp 3 def /invert false def /DeviceRGB setcolorspace
  55.     }
  56.     { readrow row }
  57.       ] def
  58.   /P4 [ { /maxv 1 def /rsize w 7 add 8 idiv def
  59.       /ncomp 1 def /invert true def
  60.     }
  61.     { f row readstring pop }
  62.       ] def
  63.   /P5 [ { readmaxv /rsize w def
  64.       /ncomp 1 def /invert false def
  65.     }
  66.     { f row readstring pop }
  67.       ] def
  68.   /P6 [ { readmaxv /rsize w 3 mul def
  69.       /ncomp 3 def /invert false def /DeviceRGB setcolorspace
  70.     }
  71.     { f row readstring pop }
  72.       ] def
  73. end
  74. /viewpbm        % filename ->
  75.  { (r) file /f exch def
  76.    f s readline pop cvn /pn exch def        % Pn
  77.    f s readline pop        % check for comment
  78.    (#) anchorsearch
  79.     { pop pop f s readline pop }
  80.    if
  81.    cvx exec /h exch def /w exch def
  82.    erasepage
  83.    /DeviceGray setcolorspace
  84.    pdict pn get dup /rprocs exch def
  85.      0 get exec            % sets maxv, rsize, ncomp, invert
  86.    /row rsize string def
  87.    /SCALE where
  88.     { pop
  89.     % Map pixels SCALE-for-1.  Assume orthogonal transformation.
  90.       w 1 0 dtransform add abs div SCALE mul
  91.       h 0 1 dtransform add abs div SCALE mul
  92.     }
  93.     {    % Scale the image (uniformly) to fit the page.
  94.       clippath pathbbox
  95.         exch abs exch abs min exch pop exch pop dup
  96.     }
  97.    ifelse scale
  98.    20 dict begin        % image dictionary
  99.      /ImageType 1 def
  100.      /Width w def
  101.      /Height h def
  102.      /ImageMatrix [w 0 0 h neg 0 h] def
  103.      /BitsPerComponent maxv 1 add ln 2 ln div 0.0001 sub ceiling cvi def
  104.      /Decode [ 0 255 maxv div invert { exch } if ncomp 1 sub { 2 copy } repeat ] def
  105.      /DataSource rprocs 1 get def
  106.    currentdict end
  107.    image
  108.    showpage
  109.  } def
  110.  
  111. % If the program was invoked from the command line, run it now.
  112. [ shellarguments
  113.  { counttomark 1 ge
  114.     { ] { viewpbm } forall
  115.     }
  116.     { cleartomark
  117.       (Usage: gs -- viewpbm.ps filename.p*m ...\n) print
  118.       ( e.g.: gs -- viewpbm.ps my.ppm another.ppm\n) print flush
  119.     }
  120.    ifelse
  121.  }
  122.  { pop
  123.  }
  124. ifelse
  125.